/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gene.core.collection;

import gene.core.Component;

/**
 *
 * @author vlm
 */
public class ComponentCollection {

    public void add(Component c) {

        if (Component.head == null) {
            Component.head = c;
            Component.ground = c;
        } else {
            Component.ground.next = c;
            Component.ground = c;
        }

    }

    
    public void rem(Component c) {

        if (Component.head.equals(c)) {
            Component.head = Component.head.next;
        } else {

            Component tmp = Component.head.next;

            while (tmp.next != null) {
                if (tmp.next.equals(c)) {
                    tmp.next = tmp.next.next;
                }
            }
        }

    }

    public Component get(int index) {
        int cont=0;

        Component tmp = Component.head;
        for(;cont <= index;cont++){
        tmp = tmp.next;
        }
        return tmp;
        
    }

    /**
     * update all instancied components
     */
    public void update() {

        Component tmp = Component.head;

        while(tmp!=null){
         tmp.update();
         tmp.updateTick();
         tmp = tmp.next;
        }
        
    }

    /**
     * verify if the some component collids with any one
     */
    public void collids(){

        Component pivot = Component.head;
        Component tmp = null;
        while(pivot!=null){
            tmp=pivot.next;
            while(pivot!=null){
                tmp.collidsWith(pivot);
                tmp = tmp.next;
            }
        pivot = pivot.next;
        }

        

    }


    /**
     * draw all componets on screen buffer
     */
    public void draw() {
        

        Component tmp = Component.head;
        while(tmp!=null){
         tmp.draw();
         tmp = tmp.next;
        }

    }

    /**
     * set the start state, position and other variables for the collection;
     */
    public void start() {

        Component tmp = Component.head;
        while(tmp!=null){
         tmp.start();
         tmp = tmp.next;
        }


    }

    /**
     * destory all components instances
     */
    public void destroy() {
        Component tmp = Component.head;
        while(tmp!=null){
         tmp.destroy();
         tmp = tmp.next;
        }

    }
}
